const Service = require('egg').Service;

class UserService extends Service {
  async find(uid) {
    const { ctx } = this;
    const user = await ctx.model.User.findByPk(uid, { raw: true });
    return user;
  }

  async setsocketim(msgbox) {
    const { ctx } = this;
    const { to, from, touser, tofrom, message } = msgbox;
    if (!(to && from && touser && tofrom && message)) {
      console.info('错误');
      return ctx.body = { success: false, msg: '请传入指定参数' };
    }
    const ntouser = JSON.stringify(touser);
    const nfromuser = JSON.stringify(tofrom);
    try {
      console.info('存档');
      await ctx.model.Socketim.create({
        to,
        tofrom: nfromuser,
        from,
        message,
        touser: ntouser,
        sendTime: Date.now(),
      });
      console.info('成功');
    } catch (e) {
      console.info('失败');
    }
  }
}

module.exports = UserService;
